package lablayer.model.MathComponents;

import lablayer.model.LabData;
/**
 * Component #1
 * 
 * Middle value of array
 */
class MiddleValue implements MathComponent {
	private LabData result;
	
	private LabData data;
	
	private String componentName = "Среднее значение";
	
	private String componentId = "1";
	
	/*
	 * 
	 */
	public MiddleValue() {
		super();
		this.result = new LabData();
	}
	
	/*
	 * calculate middle value of array
	 */
        @Override
	public void calculate() {		
		Double[][][] data = this.data.getData();
		Double[] row = null;
		
		if(data.length >= 1) {
                    if(data[0].length >= 1) {
                        row = data[0][0];
                    }
		}		
		try {
                    assert row != null : "MiddleValue.calculate()";
                    double value = 0;
                    for(int i = 0; i < row.length; i++) {
                        value += row[i];
                    }
                    value /= row.length;
                    this.result.setData(value);
		} catch(Exception e) {
                    //e.printStackTrace();
		}
	}
	
	/*
	 * 
	 */
        @Override
	public String getComponentId() {
		return this.componentId;
	}
	
	/*
	 * 
	 */
        @Override
	public String getComponentName() {
		return this.componentName;
	}

        @Override
	public LabData getData() {
		return this.data;
	}

        @Override
	public void setData(LabData data) {
		this.data = data;
	}

        @Override
	public LabData getResult() {
		return this.result;
	}

	@Override
	public void setParameter(String params, String values) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public LabData getInitData() {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
